
<div class="easyui-layout" data-options="fit:true"  style="margin:5px">
    <!-- Begin of toolbar -->
    <div data-options="region:'center',border:false">
    <!-- Begin of 工具条 -->
    <div id="user-toolbar" style="margin:5px">
        <div>
        <@shiro.hasPermission name="User:create">  
        	<a href="javascript:;" class="easyui-linkbutton" iconCls="icon-add" onclick="openAddUser()" plain="false">添加</a>
        </@shiro.hasPermission>
        <@shiro.hasPermission name="User:update">      
            <a href="javascript:;" class="easyui-linkbutton" iconCls="icon-edit" onclick="openEditUser()" plain="false">修改</a>
        </@shiro.hasPermission>
        <@shiro.hasPermission name="User:delete">      
            <a href="javascript:;" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeUser()" plain="false">删除</a>
        </@shiro.hasPermission>
        <@shiro.hasPermission name="User:lock">    
            <a href="javascript:;" class="easyui-linkbutton" iconCls="icon-cancel" onclick="lockUser()" plain="false">冻结</a>
        </@shiro.hasPermission>      
            &nbsp;<label>账号：</label><input name="userName" class="easyui-textbox" id="uname"  style="width:100px">
            <label>手机号：</label><input name="mobileNum" class="easyui-textbox" id="tel"  style="width:100px">
            <label>最后登录：</label><input name="startDate" id="date_s" class="easyui-datebox" style="width:100px">
            					-<input name="endDate" id="date_e" class="easyui-datebox" style="width:100px">
            <a href="javascript:;" id ="user-search" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
        </div>
    </div>
    <!-- end of 工具条-->
	<table id="user-datagrid"></table>
    </div>
</div>
<!-- Begin of 新增、编辑 -->
<div id="user-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
	<form id="user-form" method="post">
        <table>
        	<input id="userId" name="userId" type="hidden"  class="easyui-textbox" />
        	 <tr>
                <td colspan=2 align="center" style="color:#d15b47">账户初始密码为：123456</td>
            </tr>
            <tr>
                <td width="60" align="right">账号:</td>
                <td>
                
                <input style="width:200px" data-options="required:true,validType:['length[4,8]','userName','checkname[$(\'#userId\'),\'user/validataUsername\']']"
				 type="text" name="username" class="easyui-textbox" />
				
				</td>
            </tr>
            <tr>
                <td align="right">姓名:</td>
                <td><input style="width:200px" data-options="required:true,validType:'userName'" type="text" name="name" class="easyui-textbox" /></td>
            </tr>
            <tr>
                <td align="right">手机:</td>
                <td><input style="width:200px" data-options="required:true" type="text" name="mobileNumber" class="easyui-textbox" /></td>
            </tr>
            <tr>
                <td align="right">邮箱:</td>
                <td><input style="width:200px"  data-options="validType:'email'" type="text" name="email" class="easyui-textbox" /></td>
            </tr>
            <tr>
                <td align="right">角色类型:</td>
                <td>
                	<select style="width:200px" data-options="required:true,editable:false" name="roleId" class="easyui-combobox" name="language">
						 <#list rlist as r>
				         	<option value="${r.roleId}">${r.roleName}-${r.roleDescription }</option>
				         </#list>
                	</select>
                </td>
            </tr>
            <tr>
                <td align="right">所属部门:</td>
                <td>
                	<select id="deptTree" name="deptId"    style="width:200px;"></select>
                </td>
            </tr>
             <tr>
                <td align="right">备注:</td>
                <td><input style="width:200px" data-options="validType:'length[1,30]'" type="text" name="comment" class="easyui-textbox" /></td>
            </tr>
        </table>
        <input type="hidden" name="userId"/>
    </form>
</div>
<!-- End of 新增、编辑 -->
<script type="text/javascript">

	$(function(){
		//搜索
		$("#user-search").click(function(){
			$("#user-datagrid").datagrid('load', {
				userName: $("#uname").val(),
				mobileNum: $("#tel").val(),
				startDate: $("#date_s").val(),
				endDate:	$("#date_e").val()
			});
		});
		
		//树菜单
	    $("#deptTree").combotree({
	        url: 'dept/getDeptComTree',
	        required: false,
	        editable:false,
	        onlyLeafCheck:false
	    });
	})
	/**
	* 用户 删除记录
	*/
	function removeUser(){
		var items = $('#user-datagrid').datagrid('getSelections');
		if(items.length >0){
			$.messager.confirm('信息提示','确定要删除该记录？', function(result){
				if(result){
					var ids = "";
					$(items).each(function(){
						ids += this.user_id+","
					});
					$.ajax({
						url:'user/delUser',
						data:{"ids":ids},
						success:function(data){
							if(data){
								$.messager.alert('信息提示','删除成功！','info');		
								$('#user-datagrid').datagrid('reload');  
							}
							else
							{
								$.messager.alert('信息提示','删除失败！','info');		
							}
						}	
					});
				}	
			});
		}else{
			$.messager.alert('信息提示','请至少选择一条记录！','info');		
		}
	}
	
	/**
	* 用户 打开添加窗口
	*/
	function openAddUser(){
		$('#user-form').form('clear');
		$('#user-dialog').dialog({
			closed: false,
			modal:true,
            title: "用户新增",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: addUser
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#user-dialog').dialog('close');                    
                }
            }]
        });
	}
	
	/**
	* 用户 打开修改窗口
	*/
	function openEditUser(){
		$('#user-form').form('clear');
		var item = $('#user-datagrid').datagrid('getSelections');
		if(item && item.length ==1){
			//加载表单数据
			$('#user-form').form('load', "user/getUserById?uid="+item[0].user_id);
			$('#user-dialog').dialog({
				closed: false,
				modal:true,
	            title: "用户修改",
	            buttons: [{
	                text: '确定',
	                iconCls: 'icon-ok',
	                handler: editUser
	            }, {
	                text: '取消',
	                iconCls: 'icon-cancel',
	                handler: function () {
	                    $('#user-dialog').dialog('close');                    
	                }
	            }]
	        });
		}else{
			$.messager.alert('信息提示','请选中一条记录进行修改！','info');	
		}
		
	}	
	
	
	/**
	*  添加记录
	*/
	function addUser(){
		$('#user-form').form('submit', {
			url:'user/addUser',
			success:function(data){
				if(data){
					$.messager.alert('信息提示','提交成功！','info');
					$('#user-dialog').dialog('close');
					$('#user-datagrid').datagrid('reload');  
				}
				else
				{
					$.messager.alert('信息提示','提交失败！','info');
				}
			}
		});
	}
	
	/**
	*  修改记录
	*/
	function editUser(){
		$('#user-form').form('submit', {
			url:'user/editUser',
			success:function(data){
				if(data){
					$.messager.alert('信息提示','提交成功！','info');
					$('#user-dialog').dialog('close');
					$('#user-datagrid').datagrid('reload');  
				}
				else
				{
					$.messager.alert('信息提示','提交失败！','info');
				}
			}
		});
	}
	/**
	*  冻结用户
	*/
	function lockUser(){
		var items = $('#user-datagrid').datagrid('getSelections');
		if(items.length == 1){
			$.messager.confirm('信息提示','确定要冻结该用户？', function(result){
				if(result){
					var ids = "";
					var status="";
					$(items).each(function(){
						ids += this.user_id;
						status+=this.status;
					});
					$.ajax({
						url:'user/toLockUser',
						data:{"userId":ids,"status":status},
						success:function(data){
							if(data){
								$.messager.alert('信息提示','冻结成功！','info');		
								$('#user-datagrid').datagrid('reload');  
							}
							else
							{
								$.messager.alert('信息提示','冻结失败！','info');		
							}
						}	
					});
				}	
			});
		}else{
			$.messager.alert('信息提示','请选择一条记录！','info');		
		}
	}
	
	/**
	* 载入数据
	*/
	$("#user-datagrid").datagrid({
		url:'user/userlist',
		rownumbers:true,
		singleSelect:false,
		pageSize:30,           
		pagination:true,
		multiSort:true,
		fitColumns:true,
		fit:true,
		toolbar: "#user-toolbar",
		columns:[[
			{ field:'username',title:'账号',width:100},
			{ field:'name',title:'昵称',width:180},
			{ field:'mobile_number',title:'手机号',width:100},
			{ field:'latest_login_time',title:'最后登录时间',width:100,
				formatter: function(value,row,index){
					if(row.latest_login_time){
						return date_format(row.latest_login_time.time);
					}else{
						return  "--";
					}
				}		
			},
			{ field:'deptname',title:'所属部门',width:100,
				formatter: function(value,row,index){
					if(value){
						return value;
					}else{
						return  "--";
					}
				}		
			},
			{ field:'role_name',title:'角色名',width:100},
			{ field:'status',title:'状态',width:100,			
				formatter: function(value,row,index){
					if (value == "1"){
						return "正常";
					} else {
						return "<font color='#d15b47'>已冻结</font>";
					}
				}
			},
			{ field:'czmm',title:'密码重置',width:100,			
				formatter: function(value,row,index){
						return "<a style='color:blue' href='javascript:resetpass("+row.user_id+")'>密码重置</a>";
				}
			}
		]]
	});
	
	
	function resetpass(uid){
		$.messager.confirm('信息提示','确定要重置密码为123456？', function(result){
			if(result){
				$.ajax({
					url:'user/resetPass',
					data:{"userId":uid},
					success:function(data){
						if(data){
							$.messager.alert('信息提示','重置成功！','info');		
							$('#user-datagrid').datagrid('reload');  
						}
						else
						{
							$.messager.alert('信息提示','系统出错！','info');		
						}
					}	
				});
			}
			
			
		})
	}
</script>
